package com.test.service.impl;

import com.test.controller.Admininfo.MyObject;
import com.test.controller.Admininfo.ResCode;
import com.test.controller.Admininfo.ResData;
import com.test.entity.CwInfo;
import com.test.dao.CwInfoDao;
import com.test.service.CwInfoService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 财务(CwInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-11-12 11:01:47
 */
@Service("cwInfoService")
public class CwInfoServiceImpl implements CwInfoService {
    @Resource
    private CwInfoDao cwInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CwInfo queryById(Integer id) {
        return this.cwInfoDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param cwInfo 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public ResData queryByPage(CwInfo cwInfo, PageRequest pageRequest) {
        int total =Integer.parseInt( String.valueOf(this.cwInfoDao.count(cwInfo)));
        List<CwInfo> cwInfoList = cwInfoDao.queryAllByLimit(cwInfo, pageRequest);
        int pageSize = pageRequest.getPageSize();
        int page;
        page = (total%pageSize==0)? total/pageSize : total/pageSize+1;
        List<MyObject> myObjectList = cwInfoList.stream().map(cwInfo1 -> new MyObject(cwInfo1)).collect(Collectors.toList());
        return new ResData(ResCode.SUCCESS,myObjectList,total,page);
    }

    /**
     * 新增数据
     *
     * @param cwInfo 实例对象
     * @return 实例对象
     */
    @Override
    public CwInfo insert(CwInfo cwInfo) {
        this.cwInfoDao.insert(cwInfo);
        return cwInfo;
    }

    /**
     * 修改数据
     *
     * @param cwInfo 实例对象
     * @return 实例对象
     */
    @Override
    public CwInfo update(CwInfo cwInfo) {
        this.cwInfoDao.update(cwInfo);
        return this.queryById(cwInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.cwInfoDao.deleteById(id) > 0;
    }

    @Override
    public CwInfo selectByCwInfo(String name, String password) {
        return cwInfoDao.selectByCwInfo(name,password);
    }
}
